// SeqStack.hpp
// Author: Paddy Huang
// Date: 2020.03.11 17:37

// Realized by array, can store any classes
// Index from 0
#ifndef SEQ_STACK_HXX
#define SEQ_STACK_HXX

#include <iostream>
using namespace std;

#define MAXSIZE 10

template<class T>
class SeqStack{
    public:
        SeqStack();
        ~SeqStack();
        void push(T t);
        T peek();       // Get top element
        T pop();
        const int length() noexcept;
        const bool isEmpty() noexcept;
    private:
        T *element;
        int top;    // top pointer
};

template<class T>
SeqStack<T>::SeqStack(){
    element = new T[MAXSIZE];
    if(!element)
        cout << "Stack malloc error!" << endl;
    top = -1;
}

template<class T>
SeqStack<T>::~SeqStack(){
    if(element){
        delete[] element;
        element = NULL;
        top = -1;
    }
}

template<class T>
void SeqStack<T>::push(T t){
    element[top++] = t;
}

template<class T>
T SeqStack<T>::peek(){
    return element[top - 1];
}

template<class T>
T SeqStack<T>::pop(){
    T value = element[top - 1];
    top--;
    return value;
}

template<class T>
const int SeqStack<T>::length() noexcept {
    return top;
}

template<class T>
const bool SeqStack<T>::isEmpty() noexcept {
    return top == -1;
}

#endif
